package org.jsola.contract.dao;

import org.apache.ibatis.annotations.Param;
import org.jsola.contract.entity.CategoryVariableDO;
import org.jsola.contract.query.CategoryVariableQuery;
import org.jsola.core.Page;
import org.jsola.orm.mapper.IJsolaMapper;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;


/**
 * 合同分类下模板变量
 *
 * @author JanusMix
 */
@Repository("contractCategoryVariableDAO")
public interface ICategoryVariableDAO extends IJsolaMapper<CategoryVariableDO> {


    /**
     * 查询【合同分类下模板变量】详情
     *
     * @param categoryVariableId 主键id
     * @param siteId 站点id
     * @return DO
     */
    CategoryVariableDO findById(@Param("id") Long categoryVariableId, @Param("siteId") String siteId);

    /**
     * 查询【合同分类下模板变量】列表行数
     *
     * @param categoryVariableQuery query对象
     * @param siteId 站点id
     * @return 返回行数
     */
     int findCountByQuery(@Param("query")CategoryVariableQuery categoryVariableQuery, @Param("siteId") String siteId);

    /**
     * 查询【合同分类下模板变量】列表
     *
     * @param categoryVariableQuery query对象
     * @param siteId 站点id
     * @return DO列表
     */
    List<CategoryVariableDO> findListByQuery(@Param("query")CategoryVariableQuery categoryVariableQuery, @Param("siteId") String siteId);

    /**
     * 查询【合同分类下模板变量】分页列表
     *
     * @param categoryVariableQuery query对象
     * @param startIndex 起始位置
     * @param pageSize 每页数量
     * @param siteId 站点id
     * @return DO列表
     */
    List<CategoryVariableDO> findPageListByQuery(@Param("query")CategoryVariableQuery categoryVariableQuery, @Param("startIndex") Integer startIndex,
            @Param("pageSize") Integer pageSize, @Param("siteId") String siteId);

    /**
     * 查询分页
     *
     * @param categoryVariableQuery query对象
     * @param siteId 站点id
     * @return DO分页
     */
    default Page<CategoryVariableDO> findPageByQuery(CategoryVariableQuery categoryVariableQuery, String siteId) {
        int count = this.findCountByQuery(categoryVariableQuery, siteId);
        List<CategoryVariableDO> list;
        if (count > 0) {
            list = this.findPageListByQuery(categoryVariableQuery, (categoryVariableQuery.getPageNo() - 1) * categoryVariableQuery.getPageSize(),
                    categoryVariableQuery.getPageSize(), siteId);
        } else {
            list = new ArrayList<>();
        }
        return new Page<>(list, categoryVariableQuery.getPageNo(), categoryVariableQuery.getPageSize(), count);
    }
}

